package cn.liyicun.test.controller;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.system.SystemUtil;
import cn.liyicun.test.base.Common;
import cn.liyicun.test.dto.MyResultInfo;
import cn.liyicun.test.service.UserFileService;
import cn.liyicun.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class UserFileController extends BaseController {


    @Autowired
    UserService user;
    @Autowired
    UserFileService ufile;

    @RequestMapping("/myfile")
    public String myfile(HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
        Long uid = isLogin(req, res, true);
        if (uid <= 0) {
            return null;
        }

        List<Map<String, Object>> list = ufile.myFileList(uid);
        model.addAttribute("lists", list);
        return "myfile";
    }

    @RequestMapping("/myfriend/{id}")
    public String myfriend(HttpServletRequest req, HttpServletResponse res, @PathVariable Long id, Model model) throws IOException {
        Long uid = isLogin(req, res, true);
        if (uid <= 0) {
            return null;
        }

        List<Map<String, Object>> list = ufile.myFriendList(id);
        model.addAttribute("lists", list);
        return "myfile";
    }


    @RequestMapping("/oneFile/{id}")
    public String oneFile(HttpServletRequest req, HttpServletResponse res, @PathVariable Long id, Model model) throws IOException {
        Long uid = isLogin(req, res, true);
        if (uid <= 0) {
            return null;
        }

        Map<String, Object> data = ufile.getUserFile(id);
        model.addAttribute("datas", data);

        String ip = ServletUtil.getClientIP(req);
        ufile.lookLog(uid,id,ip);
        ufile.updateLook(id);
        return "oneFile";
    }

    @RequestMapping("/likeFile")
    @ResponseBody
    public MyResultInfo fileLike(HttpServletRequest req, HttpServletResponse res, @RequestParam("id")   Long id) throws IOException {
        Long uid = isLogin(req, res);
        if (uid <= 0) {
            return MyResultInfo.error("没有登陆");
        }

        int i = ufile.updateLike(id);


        return MyResultInfo.ok(""+i);
    }


}